package mysql

import (
	"fmt"
	"gorm.io/driver/mysql"
	"gorm.io/gorm"
	"userrpc/appconfig"
)

var (
	DB  *gorm.DB
	err error
)

func MysqlInit() {
	appconf := appconfig.Appconf.Mysql
	dsn := fmt.Sprintf("%s:%s@tcp(%s:%d)/%s?charset=utf8mb4&parseTime=True", appconf.User, appconf.Password, appconf.Host, appconf.Port, appconf.Database)
	DB, err = gorm.Open(mysql.Open(dsn))
	if err != nil {
		panic(err)
	}
	fmt.Println("数据库连接成功", dsn)

	err = DB.AutoMigrate(&User{}, &People{})
	if err != nil {
		panic(err)
	}
	fmt.Println("数据库迁移成功")
}
